package com.tarena.lbs.business.dao.repository.impl;

import com.alibaba.cloud.commons.lang.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tarena.lbs.base.protocol.pager.BasePageQuery;
import com.tarena.lbs.base.protocol.pager.PageResult;
import com.tarena.lbs.business.dao.mapper.StoreMapper;
import com.tarena.lbs.business.dao.repository.StoreRepository;
import com.tarena.lbs.business.pojo.po.StorePO;
import com.tarena.lbs.business.pojo.query.StoreQuery;
import com.tarena.lbs.business.pojo.vo.StoreVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@Repository
@Slf4j
public class StoreRepositoryImpl implements StoreRepository {

    @Autowired
    StoreMapper storeMapper;

    @Override
    public PageResult<StoreVO> selectpageList(StoreQuery query) {
        //构建查询条件
        LambdaQueryWrapper<StorePO> wrapper = new LambdaQueryWrapper<>();
        //店铺名称模糊查询
        if (StringUtils.isNotBlank(query.getStoreName())){
            wrapper.like(StorePO::getStoreName, query.getStoreName());
        }
        //负责人电话模糊查询
        if (StringUtils.isNotBlank(query.getStoreHeadPhone())){
            wrapper.eq(StorePO::getStoreHeadPhone, query.getStoreHeadPhone());
        }
        //商家id查询
        if (query.getBusinessId() != null){
            wrapper.eq(StorePO::getBusinessId, query.getBusinessId());
        }
        //时间范围查询
        if (query.getStartingTime() !=null && query.getEndTime() != null) {
            wrapper.between(StorePO::getUpdateTime, query.getStartingTime(), query.getEndTime());
        }else {
            if (query.getStartingTime() != null) {
                wrapper.ge(StorePO::getUpdateTime, query.getStartingTime());
            }
            if (query.getEndTime() != null) {
                wrapper.le(StorePO::getUpdateTime, query.getEndTime());
            }
        }
        //门店状态
        if (query.getStoreStatus() !=null){
            wrapper.eq(StorePO::getStoreStatus, query.getStoreStatus());
        }

        //执行查询
        List<StorePO> poPage = storeMapper.selectList(wrapper);
        log.info("查询结果:{}", poPage);
        //转换vo
        List<StoreVO> voList = new ArrayList<>();
        for (StorePO po : poPage){
            StoreVO vo = new StoreVO();
            BeanUtils.copyProperties(po,vo);
            voList.add(vo);
        }
        BasePageQuery simplePager = new BasePageQuery(query.getPageNo(), query.getPageSize());

        PageResult<StoreVO> pageResult = new PageResult<>(simplePager);
        pageResult.setObjects(voList);
        pageResult.setTotal((long)poPage.size());
        return pageResult;
    }
    public void save(StorePO storePO) {
        log.info("新增店铺业务参数：{}", storePO);
        storeMapper.insert(storePO);
    }

    @Override
    public List<StoreVO> findByBusinessId(Integer businessId) {
        log.debug("正在查询商家下的店铺:{}",businessId);
        List<StoreVO> storeVOList = storeMapper.selectByBusinessId(businessId);
        return storeVOList;
    }

    @Override
    public void updateStatus(StorePO storePO) {
        storeMapper.updateById(storePO);
    }

    @Override
    public StoreVO findById(Integer id) {
        StorePO po = storeMapper.selectById(id);
        StoreVO storeVO = new StoreVO();
        BeanUtils.copyProperties(po,storeVO);
        return storeVO;
    }

    @Override
    public int selectByStoreName(String storeName) {
        return storeMapper.selectByStoreHeadName(storeName);
    }

    @Override
    public void delete(Integer id) {
        storeMapper.deleteById(id);
    }

    @Override
    public void updateStore(StorePO storePO) {
        storeMapper.updateById(storePO);
    }
}
